package org.hbnu.xiaomi.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.hbnu.xiaomi.entity.Orders;

import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface OrderMapper {

    // 查询用户订单
    @Select("select * from orders where user_id=#{user_id}")
    @Results({
            @Result(property = "good_id", column = "good_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "user_id", column = "user_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "ordertime", column = "ordertime", jdbcType = JdbcType.INTEGER),
            @Result(property = "paid", column = "paid", jdbcType = JdbcType.INTEGER),
            @Result(property = "paymentMethod", column = "paymentMethod", jdbcType = JdbcType.INTEGER)

    })
    public List<Orders> selectOrdersByUserId(Integer user_id);

    // 根据订单id查找商品id
    @Select("SELECT good_id FROM orders WHERE id = #{id}")
    int findGoodIdByOrderId(int id);


    // 保存用户订单
    @Insert("INSERT INTO orders (good_id, user_id, ordertime, num, price, paid, paymentMethod,pic) " +
            "VALUES (#{good_id}, #{user_id}, #{ordertime}, #{num}, #{price}, #{paid}, #{paymentMethod},#{pic})")
    @Options(useGeneratedKeys = true, keyProperty = "id") //设置自增主键
    public void insert(Orders orders);


    //删除用户订单
    @Delete("DELETE FROM orders WHERE id=#{id}")
    public void deleteById(int id);


    //收货用户订单
    @Update("UPDATE orders SET paid = #{paid} WHERE id = #{id}")
    int updateOrder(@Param("id") int id, @Param("paid") int paid);

    //收货用户订单
    @Update("UPDATE orders SET paid = #{paid} ,num=#{num} WHERE id = #{id}")
    int productOrder(@Param("id") int id, @Param("paid") int paid,@Param("num") int num);


}
